import type { ApiPageResult } from "@/api/basemodel";
import type { TablePaginationConfig } from "ant-design-vue";
import type { ComputedRef } from "vue";
import { usePagination, useLoadMore, useRequest } from "vue-request";
import type { IService } from "vue-request/dist/types/core/utils/types";

/**table page 请求封装 */
export function pageRequest(service: IService<ApiPageResult, any>) {
    const {
        data,
        current,
        pageSize,
        total,
        reload,
        refresh,
        loading,
        changePagination
    } = usePagination(service, {
        pagination: {
            currentKey: 'page',
            pageSizeKey: 'pageSize',
            totalKey: 'data.total'
        }
    });

    const dataSource = computed(() => data.value?.data.list || [])
    const pagination: ComputedRef<TablePaginationConfig> = computed(() => ({
        total: total.value,
        current: current.value,
        pageSize: pageSize.value,
        showSizeChanger: true,
        // hideOnSinglePage: true,
        onChange: changePagination
    }))

    return {
        reload,
        refresh,
        loading,
        dataSource,
        pagination
    }
}


export {
    useLoadMore,
    useRequest
}
